package mpi.v23;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.LocalActivityManager;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ViewSwitcher;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import android.util.Log;

class Rooted {
	static String TAG="Rooted";
	static public void exec(String[] cmds) {
		try {
			// String[] cmds = {"sync", "sync",  "reboot"};
			Process p = Runtime.getRuntime().exec("su");
			DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
			DataInputStream is = new DataInputStream(p.getInputStream());  
			for (String tmpCmd : cmds) {
				os.writeBytes(tmpCmd+"\n");

			}           
			os.writeBytes("exit\n");  
			os.flush();
		}
		catch (Exception e) { 
		}
	}

	static public void exec(String cmd) {
		String[] cmds = new String[1];
		cmds[0] = cmd;
		exec(cmds);
	}
	static public boolean isRooted() {
		String buildTags = android.os.Build.TAGS;
		String test = android.os.Build.MODEL;
		if (test != null && (test.contains("Kindle") || test.contains("Nook") || test.contains("Blue"))) {
			return true;
		}
		return false;
	}

}
public class AMI extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		m_context = getApplicationContext();
		getInstallerClass();
		new CopyAssets().execute();
	}

	private void showRebootDialog() {
			// bring us to the front
			Intent intent = new Intent(this, AMI.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT );//| Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
			startActivity(intent);

			// restart the machine
			new AlertDialog.Builder(AMI.this)
			.setTitle("Attempting to Re-Initialize. This may take a while...")
			.setMessage("Press OK to Continue")
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {reboot();}
			})
			.setNegativeButton("", null)
			.show();		 
			return;
	}

	private void getInstallerClass() {
		Intent intent = new Intent();
		intent.addCategory("android.intent.category.DEFAULT");
		//intent.setType("application/vnd.android.package-archive");
		intent.setDataAndType(Uri.parse("file:///dummy"),"application/vnd.android.package-archive");


		PackageManager packageManager = getApplicationContext().getPackageManager();
		// final Intent intent = new Intent(action);
		List resolveInfo =
				packageManager.queryIntentActivities(intent,0);
		if ((resolveInfo != null) && (resolveInfo.size() > 0)) {

			for (Object l: resolveInfo){
				ResolveInfo ri = (ResolveInfo)l;
				Log.d("FILTEROUT",ri.activityInfo.packageName +":"+ri.activityInfo.name);
				if (ri.activityInfo.packageName.contains("package")) {
					m_installerPkg = ri.activityInfo.packageName;
					m_installerActivity = ri.activityInfo.name;
				}

			}
		}

	}

	private String copyAssetToFile(String asset) {
		try {
			InputStream is = m_context.getAssets().open(asset);
			String dirname = "/data/downloads/";
			File path = new File(dirname);
			path.mkdirs();
			File outputFile = new File(path, asset);

			FileOutputStream fos = new FileOutputStream(outputFile);

			byte[] buffer = new byte[2048];
			int len1 = 0;
			while ((len1 = is.read(buffer)) != -1) {
				fos.write(buffer, 0, len1);
			}
			fos.close();
			is.close();
			try {
			  Process p = Runtime.getRuntime().exec("su");
			  DataOutputStream os = new DataOutputStream(p.getOutputStream());            
			  os.writeBytes("chgrp 1000 " + dirname+asset  +"\n");
			  os.writeBytes("chmod 644 " + dirname+asset  +"\n");
			  os.writeBytes("chown 1000 " + dirname+asset  +"\n");
			  if (asset.equals("g12.apk"))
			  {
			    os.writeBytes("pm install -r " + dirname+asset + "\n");
			    os.writeBytes("sleep 1 \n");
			    os.writeBytes("rm " + dirname+asset + "\n");
			  }
			  os.writeBytes("exit\n");  
			  os.flush();
			  p.waitFor();
			}
			catch (Exception e) { 
			  Log.d("XXX","error in changing permissions");
			}
			return dirname + asset;
		} catch (Exception e) {
		  Log.d(TAG,e.toString());
		  return null;
		}
	}

	private void reboot() {
	  try {
	    String[] cmds = {"sync", "sync",  "killall -9 system_server"};
	    Process p = Runtime.getRuntime().exec("su");
	    DataOutputStream os = new DataOutputStream(p.getOutputStream());            
	    for (String tmpCmd : cmds) {
	      os.writeBytes(tmpCmd+"\n");
	    }           
	    os.writeBytes("exit\n");  
	    os.flush();
	  }
	  catch (Exception e) { 
	    Log.d("XXX","reboot failed");
	  }
	}
	private class CopyAssets extends AsyncTask<String, Integer, Long> {
	  protected void onPreExecute( ){	
	    m_pd = new ProgressDialog(AMI.this);
	    m_pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

	    m_pd.setMax(m_apklist.length);
	    m_pd.setTitle("Copying Files ....");
	    m_pd.show();
	  }

	  protected Long doInBackground(String... apklist) {
	    int count = m_apklist.length;
	    int i = 0;

	    for (String f:m_apklist) {
	      Log.d(TAG, "copying:" + f);
	      String tmp = copyAssetToFile(f);
	      if (tmp == null)
		continue;
	      publishProgress(i++);
	    }

	    return new Long(i);
	  }

	  protected void onProgressUpdate(Integer... progress) {
	    m_pd.setTitle(m_apklist[progress[0]].replace(".apk", ""));
	    m_pd.incrementProgressBy(1);
	  }

	  protected void onPostExecute(Long result) {
	    m_pd.dismiss();
	    reboot();
	    //showRebootDialog();
	  }
	}

	// --------------------------------------------------------------------
	String TAG = "AndroidMarketInstallerActivity";
	private LinkedList<PkgInfo> m_Q;
	Context m_context;
	ProgressDialog m_pd;
	static String[] m_apklist = {
	  "g1.apk", "g12.apk"
	};
	String m_installerPkg, m_installerActivity;
	ViewSwitcher m_switcher;

}
